package com.indusfo.spc.untlis;

import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import javax.activation.MimetypesFileTypeMap;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author louk
 * @date 2019/10/21 8:35
 * @description 文件下载
 */
public class FileUtil {
    public static void download(String filename, HttpServletResponse res) throws IOException {

        try {

            String realFileName = getFileName(filename);
            Resource resource = new ClassPathResource("excelModel/"+realFileName+".xlsx");
            File file = resource.getFile();
            String fileName = resource.getFilename();
            InputStream inputStream = new FileInputStream(file);
            // 设置信息给客户端不解析
            String type = new MimetypesFileTypeMap().getContentType(filename);
            // 设置contenttype，即告诉客户端所发送的数据属于什么类型
            res.setHeader("Content-type",type);
            //强制下载不打开
            res.setContentType("application/force-download");
            OutputStream out = res.getOutputStream();
            //使用URLEncoder来防止文件名乱码或者读取错误
            res.setHeader("Content-Disposition", "attachment; filename="+ URLEncoder.encode(fileName, "UTF-8"));

            int b = 0;
            byte[] buffer = new byte[1000000];
            while (b != -1) {
                b = inputStream.read(buffer);
                if(b!=-1) {
                    out.write(buffer, 0, b);
                }
            }
            inputStream.close();
            out.close();
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private static  String getFileName(String filename){
        String fileName="";
        switch (filename){
            case "15":
                fileName="用户模板";
            break;
            case "2":
                fileName="物料信息模板";
                break;
            case "3":
                fileName="车间模板";
                break;
            case "4":
                fileName="产线模板";
                break;
            case "5":
                fileName="工位模板";
                break;
            case "6":
                fileName="工序模板";
                break;
            case "7":
                fileName="采集项目模板";
                break;
            case "8":
                fileName="设备模板";
                break;
            case "9":
                fileName="托盘模板";
                break;
            case "10":
                fileName="班次信息模板";
                break;
            case "11":
                fileName="条码规则模板";
                break;
            case "12":
                fileName="不良原因模板";
                break;
            case "13":
                fileName="不良对策模板";
                break;
            case "22":
                fileName="工艺文件库模板";
                break;
        }
        return fileName;
    }


}
